package com.situ.company.service.Impl.service.Impl;



import com.situ.company.dao.UserDao;
import com.situ.company.entity.User;
import com.situ.company.service.Impl.UserService;
import com.situ.company.util.StringUtil;
import com.situ.company.vo.ResultVO;
import com.situ.company.vo.TableVO;
import com.situ.company.vo.UserVO;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Pattern;

public class UserServiceImpl implements UserService {
    UserDao dao = new UserDao();

    @Override
    public TableVO select(Integer page, Integer limit, String username, String name) {
        List<UserVO> list = dao.list(page,limit,username,name);;

        //把数据返回给table小工具(响应)
        //获取一个响应的前台写出对象
        //list转完后是[{},{},{}]


        TableVO vo = new TableVO();
        //dao统计一个数，一共都少条数据
        vo.setCount(dao.count(username,name));
        vo.setData(list);
        //使用json工具把对象转成json格式的字符串
        //fastjson 和 jackson
        //通过jackson工具把list对象转成json 字符串
        return vo;


    }

    @Override
    public void save(User u ) {
        if(u.getId()==null){
            u.setCreateTime(LocalDateTime.now());
            //加密
            u.setPassword(StringUtil.md5(u.getPassword()));

            dao.insert(u);
        }else{
            dao.update(u);
        }
    }

    @Override
    public ResultVO delete(Integer id) {
        //根据id查询 返回一个集合 如果集合有值，则只有一个，那么第一个就是要查的
        //把第一个取出来判断一下是不是管理员
        List<User> list = dao.selectById(id);
        User u =list.get(0);
        ResultVO vo = new ResultVO();
        if(u.getUsername().equals("admin")){
                vo.setCode(2);
                vo.setMsg("此账号是管理员账号，不能删除");
        }else{

        dao.delete(id);
        }
        return  vo;
    }


    //编辑时把所有信息罗列出来
    @Override
    public User info(Integer id) {
        List<User> list =dao.edit(id);
        if(list.isEmpty()){
            return null;
        }else {
            return list.get(0);
        }
    }
    //检验账号密码是否正确
    @Override
    public boolean check(String username, String password) {
        List<User> list = dao.selectByUsername(username);
        if(list.isEmpty()){//用户名不对
            return false;
        }
        User u = list.get(0);
          //  return u.getPassword().equals(password);
        //加密2
        return u.getPassword().equals(StringUtil.md5(password));
    }
    //检验用户表中新增用户的密码格式是否正确
    @Override
    public ResultVO check2(String password) {
        ResultVO vo =new ResultVO();
        boolean b = checkPasswordFormat(password);
        if(password.length()<6){
            vo.setCode(1);
        }else if(!b){
            vo.setCode(2);
        }
        return vo;
    }

    //重置密码  判断两次密码输入是否一致，判断密码格式，长度
    @Override
    public ResultVO resetPassword(String newpassword1, String newpassword2, String s) {
        ResultVO vo = new ResultVO();
        boolean b = checkPasswordFormat(newpassword1);
        if(newpassword1.length()<6){
            vo.setCode(3);
        }
        else if (!b){
            vo.setCode(2);
        }
        else if(!newpassword1.equals(newpassword2)){
                vo.setCode(0);
        }else{
            dao.resetPassword(newpassword1,s);
                vo.setCode(1);

        }
        return vo;
    }


    //检查密码格式
    private boolean checkPasswordFormat(String newpassword1) {
        String pattern = "^(?=.*[0-9])(?=.*[a-zA-Z])[0-9A-Za-z]{6,}$";
        return Pattern.matches(pattern, newpassword1);

    }


    //检查密码
    @Override
    public ResultVO checkPassword(String password,String s) {


        List<User> list = dao.selectByUsername(s);
        User u = list.get(0);
        ResultVO vo = new ResultVO();
        if(!u.getPassword().equals(StringUtil.md5(password))){
            vo.setCode(0);
        }else{
            vo.setCode(1);
        }
        return vo;
    }

    @Override
    public ResultVO checkByUsername(String username) {
        List<User> list = dao.selectByUsername(username);
        ResultVO vo =new  ResultVO();
        boolean b = checkUsernameFormat(username);
        if(!list.isEmpty()){//集合不为空用户名重复
            vo.setCode(1);
        } else if (!b) {//用户名格式不符合要求
            vo.setCode(2);
        }
        return vo;
      //  return list.isEmpty()? null : list.get(0);
    }


    //检查用户名格式
    private boolean checkUsernameFormat(String username) {
        String pattern = "^(?=.*[0-9])(?=.*[a-zA-Z])[0-9A-Za-z]{6,}$";
        return Pattern.matches(pattern, username);

    }


}
